package cc.vv.lklibrary.db.help;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.text.TextUtils;

import com.j256.ormlite.android.apptools.OrmLiteSqliteOpenHelper;
import com.j256.ormlite.support.ConnectionSource;
import com.j256.ormlite.table.TableUtils;

import java.sql.SQLException;

import cc.vv.lklibrary.log.LogOperate;

/**
 * 数据库创建、更新基类
 *
 * @author NPF
 * @date 2018/2/27.
 */
public abstract class DBBaseOpenHelper extends OrmLiteSqliteOpenHelper {
    /**
     * 需要初始化的属性
     */
    private static Context mContext;
    /**
     * 所有数据表的映射实体类的字节码对象
     */
    private Class[] mTableBeanClass;

    /**
     * 构造进行数据库信息初始化
     *
     * @param dbName         数据库名称
     * @param dbVersion      数据库版本
     * @param tableBeanClass 需要创建的表的映射实体字节码
     */
    public DBBaseOpenHelper(String dbName, int dbVersion, Class... tableBeanClass) {
        this(mContext, dbName, null, dbVersion);
        mTableBeanClass = tableBeanClass;
        if (null == mContext) {
            throw new RuntimeException("必须在Application中调用DBBaseOpenHelper.initInstance()");
        }
        if (TextUtils.isEmpty(dbName)) {
            throw new RuntimeException("DBBaseOpenHelper中数据库名 dbName 不能为空");
        }

    }

    /**
     * 继承自父类构造,私有化禁止子类调用
     *
     * @param context         上下文
     * @param databaseName    数据库名
     * @param factory         SQLiteDatabase.CursorFactory
     * @param databaseVersion 版本
     */
    private DBBaseOpenHelper(Context context, String databaseName, SQLiteDatabase.CursorFactory factory, int databaseVersion) {
        super(context, databaseName, factory, databaseVersion);
    }

    /**
     * 初始化,需要在Application中进行初始化
     *
     * @param context 上下文
     */
    public static void initInstance(Context context) {
        mContext = context;
    }

    /**
     * 数据库表创建
     *
     * @param database         SQLiteDatabase
     * @param connectionSource ConnectionSource
     */
    @Override
    public void onCreate(SQLiteDatabase database, ConnectionSource connectionSource) {
        try {
            if (null != mTableBeanClass && mTableBeanClass.length > 0) {
                for (Class tableBeanClass : mTableBeanClass) {
                    int tableIfNotExists = TableUtils.createTableIfNotExists(connectionSource, tableBeanClass);
                    LogOperate.e("创建表====" + tableIfNotExists + "===" + tableBeanClass);
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    /**
     * 数据库更新
     *
     * @param database         SQLiteDatabase
     * @param connectionSource ConnectionSource
     * @param oldVersion       数据库旧版本
     * @param newVersion       数据库新版本
     */
    @Override
    public void onUpgrade(SQLiteDatabase database, ConnectionSource connectionSource, int oldVersion, int newVersion) {
        update(database, connectionSource, oldVersion, newVersion);
    }

    /**
     * 子类实现,主要编写在数据库进行更新时需要进行的操作,可以借助DBTableOperate操作类进行处理
     *
     * @param database         SQLiteDatabase
     * @param connectionSource ConnectionSource
     * @param oldVersion       数据库旧版本
     * @param newVersion       数据库新版本
     */
    protected abstract void update(SQLiteDatabase database, ConnectionSource connectionSource, int oldVersion, int newVersion);
}